﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using ARMS.Models;
using ARMS.BaseClasses;

namespace ARMS.Controllers
{
    public class WorkOrderController : Controller
    {
        //
        // GET: /WorkOrder/

        //List all Work Orders
        public ActionResult Index()
        {
            try
            {
                DataTable results = new DataTable();
                //get a list of all work orders
                WorkOrderModel newWorkOrderDA = new WorkOrderModel();
                results = newWorkOrderDA.listWorkOrder();

                return View("Index", results);
             }
            catch (Exception e)
            {
                return View("Error", e);
            }
        }
          
        [HttpPost]
        public ActionResult View(int WorkOrderID)
        {
            try
            {
                DataSet results = new DataSet();
     
                WorkOrderModel newWorkOrderDA = new WorkOrderModel();
                results = newWorkOrderDA.returnWorkOrder(Convert.ToInt32(WorkOrderID));

                return View("View", results);
            }
            catch (Exception e)
            {
                return View("Error", e);
            }
        }

        [HttpPost]
        public ActionResult Delete(int WorkOrderID)
        {
            int affectedRows = 0;

            try
            {
                WorkOrderModel newWorkOrderDA = new WorkOrderModel();
                affectedRows = newWorkOrderDA.deleteWorkOrder(Convert.ToInt32(WorkOrderID));

                if (affectedRows == 1)
                {
                    ViewData["string"] = "One record was deleted";
                    return View("Delete");
                }
                else
                {

                    ViewData["string"] = "No records were deleted";
                    return View("Delete");
                }
            }
            catch (Exception e)
            {
                ViewData["exception"] = e.ToString();
                return View("Error");
            }
        }

        [HttpPost]
         public ActionResult Update(int WorkerID, string Status, string WorkNote, int WorkOrderID)
        {           
            int affectedRows = 0;

            WorkOrderModel newWorkOrderDA = new WorkOrderModel();
            affectedRows = newWorkOrderDA.updateWorkOrder(WorkerID, Status, WorkNote, WorkOrderID);

            DataSet results = new DataSet();
            results = newWorkOrderDA.returnWorkOrder(Convert.ToInt32(WorkOrderID));

            if (affectedRows == 1)
            {
                return View("View", results);
            }
            else
            {
                //Did not update record
                return View("Error");
            }
        }

        [HttpPost]
            public ActionResult Create(int BookingID, int CustomerID, int WorkerID, string Status, string WorkNote)
            {
                int affectedRows = 0;

                WorkOrderModel newWorkOrderDA = new WorkOrderModel();
                affectedRows = newWorkOrderDA.createWorkOrder(BookingID, CustomerID, WorkerID, Status, WorkNote);

                if (affectedRows == 1)
                {
                    return View("Create");
                    //success
                }
                else
                {
                    //Fail
                    return View("Error");
                }
        }

        [HttpPost]
        public ActionResult ListParts()
        {
            try
            {
                DataTable results = new DataTable();
                WorkOrderModel newWorkOrderDA = new WorkOrderModel();
                
                //list Logic
                results = newWorkOrderDA.listWorkOrder();

                return View("ListParts", results);
            }
            catch (Exception e)
            {
                return View("Error", e);
            }
        }

        [HttpPost]
        public ActionResult ViewPart()
        {
            try
            {
                DataTable results = new DataTable();
                WorkOrderModel newWorkOrderDA = new WorkOrderModel();

                //View Logic
                results = newWorkOrderDA.listWorkOrder();

                return View("ViewPart", results);
            }
            catch (Exception e)
            {
                return View("Error", e);
            }
        }

        [HttpPost]
        public ActionResult DeletePart()
        {
            try
            {
                DataTable results = new DataTable();
                //Delete Logic
                WorkOrderModel newWorkOrderDA = new WorkOrderModel();
                results = newWorkOrderDA.listWorkOrder();

                //List logic
                results = newWorkOrderDA.listWorkOrder();

                return View("ListParts", results);
            }
            catch (Exception e)
            {
                return View("Error", e);
            }
        }

        [HttpPost]
        public ActionResult AddPart()
        {
            try
            {
                DataTable results = new DataTable();
                WorkOrderModel newWorkOrderDA = new WorkOrderModel();

                //Add Logic
                results = newWorkOrderDA.listWorkOrder();

                //List logic
                results = newWorkOrderDA.listWorkOrder();

                return View("ListParts", results);
            }
            catch (Exception e)
            {
                return View("Error", e);
            }
        }

        [HttpPost]
        public ActionResult UpdatePart()
        {
            try
            {
                DataTable results = new DataTable();
                WorkOrderModel newWorkOrderDA = new WorkOrderModel();

                //Update Logic
                results = newWorkOrderDA.listWorkOrder();

                //List logic
                results = newWorkOrderDA.listWorkOrder();

                return View("ListParts", results);
            }
            catch (Exception e)
            {
                return View("Error", e);
            }
        }
    }
}
